﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEditor;
using System.IO;

namespace Nirvana
{
    /// <summary>
    /// assetbundle资源抽象类
    /// </summary>
    [Serializable]
    public struct AssetID : IEquatable<AssetID>
    {
        public static readonly AssetID Empty = new AssetID(string.Empty, string.Empty);
        [SerializeField]
        private string bundlename;
        [SerializeField]
        private string assetname;
        [SerializeField]
        private string assetguiid;
        public string BundleName { get { return bundlename; }  set { bundlename = value; } }
        public string AssetName { get { return assetname; } set { assetname = value; } }
        public string AssetGUID { get { return assetguiid; } set { assetguiid = value; } }
        public AssetID(string bundleName,string assetName)
        {
            this.bundlename = bundleName;
            this.assetname = assetName;
            this.assetguiid = string.Empty;
        }
        public bool Equals(AssetID other)
        {
            return this.bundlename == other.BundleName && this.assetname == other.AssetName;
        }

        public bool IsEmpty { get { return string.IsNullOrEmpty(this.bundlename) || string.IsNullOrEmpty(assetname); } }
        public override int GetHashCode()
        {
            int hashcode = this.bundlename.GetHashCode();
            return 397 * hashcode ^ this.assetname.GetHashCode();
        }

        public override string ToString()
        {
            return this.bundlename + ":" + this.assetname;
        }
        public static AssetID Parse(string txt)
        {
            if (string.IsNullOrEmpty(txt))
            {
                return default(AssetID);
            }
            int index = txt.IndexOf(':');
            if(index >= 0)
            {
                string _bundlename = txt.Substring(0, index);
                string _assetname = txt.Substring(index);
                return new AssetID(_bundlename, _assetname);
            }
            throw new FormatException("Can not parse AssetID");
        }

        public T LoadObject<T>() where T : UnityEngine.Object
        {
#if UNITY_EDITOR
            string path = this.GetAssetPath();
            if (!string.IsNullOrEmpty(path))
            {
                return AssetDatabase.LoadAssetAtPath<T>(path);
            }
#endif
            return (T)((object)null);
        }
#if UNITY_EDITOR
        public string GetAssetPath()
        {
            if (this.IsEmpty) return null;
            string path = null;
            if (!string.IsNullOrEmpty(this.assetguiid))
            {
                path = AssetDatabase.GUIDToAssetPath(this.AssetGUID);
            }

            if (string.IsNullOrEmpty(path))
            {
                string[] assetnames = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(this.bundlename, Path.GetFileNameWithoutExtension(this.assetname));
                if (Path.HasExtension(this.assetname))
                {
                    string extension = Path.GetExtension(this.assetname);
                    foreach(var s in assetnames)
                    {
                        if(Path.GetExtension(s) == extension)
                        {
                            path = s;
                            break;
                        }
                    }
                }
                else if(assetnames.Length > 0)
                {
                    path = assetnames[0];
                }

            }
            return path;
        }
#endif

        public void RefreshAssetBundleName()
        {
#if UNITY_EDITOR
            string path = AssetDatabase.GUIDToAssetPath(this.assetguiid);
            AssetImporter import = AssetImporter.GetAtPath(path);
            if(import != null)
            {
                this.bundlename = import.assetBundleName;
                this.assetname = path.Substring(path.LastIndexOf("/") + 1);
            }
#endif
        }

    }
}

